package com.gupaoedu.task.pattern.proxy.userservicecglib;



import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class UserServiceCglib implements MethodInterceptor {
    Object getInstance(Class<?> clazz)throws Exception{
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        methodProxy.invokeSuper(o, objects);
        atfer();
        return null;
    }
    private void before() {
        System.out.println("事务开始。。。");
    }
    private void atfer() {
        System.out.println("事务结束。。。");
    }


}
